package com.txl.web.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.txl.domain.PasswordForm;
import com.txl.domain.UserBean;
import com.txl.service.UserService;
import com.txl.utils.WebUtils;

public class UpdatePasswordServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PasswordForm pf = WebUtils.request2Bean(request, PasswordForm.class);
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		if(user==null){//如果用户没有登录
			request.setAttribute("message", "请您先登录！");
			request.getRequestDispatcher("/message.jsp").forward(request, response);
			return;
		}
		UserService service = new UserService();
		user= service.updatePasswrod(user, pf);//修改密码，返回修改后的用户对象
		if(user==null){//数据不合法
			request.setAttribute("pf", pf);
			request.getRequestDispatcher("/page/updatepassword.jsp").forward(request, response);
			return;
		}
		request.getSession().setAttribute("user", user);
		request.setAttribute("message", "密码修改成功！");
		request.getRequestDispatcher("/message.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}

